package com.aaa.os.controller;

import com.aaa.common.entity.*;
import com.aaa.common.util.ResultStatus;
import com.aaa.common.vo.ProductOrderVO;
import com.aaa.os.service.*;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import com.aaa.common.util.BaseUtil;
import com.aaa.common.vo.Page;
import com.aaa.common.util.Result;
import lombok.extern.log4j.Log4j2;

import java.util.Map;

/**
 * 订单表(ProductOrder)表控制层
 *
 * @author makejava
 * @since 2025-09-22 11:31:52
 */
@RestController
@RequestMapping("productOrder")
@Log4j2
public class ProductOrderController {
    /**
     * 服务对象
     */
    @Resource
    private ProductOrderService productOrderService;
    @Resource
    private PayService payService;
    @Resource
    private RemoteUserIntegralService remoteUserIntegralService;
    @Resource
    private RemoteUserIntegralLogService remoteUserIntegralLogService;
    @Resource
    private RemoteWashCarCouponService remoteWashCarCouponService;

    /**
     * 获取支付状态
     * @param orderNo
     * @return
     */
    @GetMapping("getPayStatus/{orderNo}")
    public Result getPayStatus(@PathVariable("orderNo") String orderNo) {
        Map<String, String> map = payService.getPayStatus(orderNo);
        return BaseUtil.success(map);
    }
    /**
     * 生成支付二维码
     * @param productOrder
     * @return
     */
    @PostMapping("getNativeCodeUrl")
    public Result getNativeCodeUrl(@RequestBody ProductOrder productOrder){
        Map map=payService.getNativeByOrder(productOrder);
        return BaseUtil.success(map);
    }

    @PostMapping("queryOrder")
    public Result queryOrder(@RequestBody Page<ProductOrderVO> page) {
        return BaseUtil.success(this.productOrderService.queryOrder(page));
    }

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @return 查询结果
     */
    @PostMapping("queryProductAndOrder")
    public Result queryByPage(@RequestBody Page<ProductOrderVO> page) {
        return BaseUtil.success(this.productOrderService.queryProductAndOrder(page));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Result selectOne(Integer id) {
        return BaseUtil.success(this.productOrderService.queryById(id));
    }

    @GetMapping("queryById")
    public ProductOrderVO queryById(@RequestParam("id") Integer id) {
        return this.productOrderService.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param productOrder 实体
     * @return 新增结果
     */
    @PostMapping("insert")
    public Result add(@RequestBody ProductOrder productOrder) {
        ProductOrder saveProductOrder = this.productOrderService.insert(productOrder);
        // 判断支付方式
        String payType = saveProductOrder.getPayType();
        if ("0".equals(payType)) {      //微信支付
            return BaseUtil.success(saveProductOrder);
        }else if ("1".equals(payType)) {    //积分支付
            //校验用户积分
            //新建用户积分对象
            UserIntegral userIntegral = new UserIntegral();
            userIntegral.setUserId(saveProductOrder.getUserId());
            userIntegral.setStoreId(saveProductOrder.getStoreId());
            //远程调用，查询用户积分对象
            UserIntegral userIntegral1 = remoteUserIntegralService.selectUserStoreIntegral(userIntegral);
            //积分id
            if (userIntegral1 == null) {
                // 用户在该门店无积分记录，直接返回对应错误
                return new Result(ResultStatus.NO_STORE_INTEGRAL.getReturnCode(),
                        ResultStatus.NO_STORE_INTEGRAL.getReturnMessage(),
                        saveProductOrder);
            }
            //用户积分id
            Integer integralId = userIntegral1.getIntegralId();
            //用户积分
            Integer credits = userIntegral1.getCredits();
            //获取用户积分
            // 补充credits为null的处理（默认0）
            credits = (credits == null) ? 0 : credits;

            //商品积分
            Double totalIntegral = saveProductOrder.getTotalIntegral();
            // 处理totalIntegral可能为null的情况（默认0，但实际积分支付时应>0）
            totalIntegral = (totalIntegral == null) ? 0.0 : totalIntegral;
            //积分支付
            //积分小于订单积分
            if(credits < totalIntegral){
                //积分不足
                return new Result(ResultStatus.INTEGRAL_NOT_ENOUGH.getReturnCode(), ResultStatus.INTEGRAL_NOT_ENOUGH.getReturnMessage(), saveProductOrder);
            }else{
                // 调用Service层的积分支付方法
                return productOrderService.integralPay(saveProductOrder, integralId, credits,totalIntegral);
            }
        }else {
            return BaseUtil.fail(saveProductOrder);
        }
    }

    /**
     * 编辑数据
     *
     * @param productOrder 实体
     * @return 编辑结果
     */
    @PostMapping("/updateOrder")
    public Result updateOrder(@RequestBody ProductOrder productOrder) {
        return BaseUtil.success(this.productOrderService.updateOrder(productOrder));
    }

    @PostMapping("/update")
    public Result update(@RequestBody ProductOrder productOrder) {
        return BaseUtil.success(this.productOrderService.update(productOrder));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("deleteById")
    public Result deleteById(Integer id) {
        return BaseUtil.success(this.productOrderService.deleteById(id));
    }

}

